package com.ccx.coachsite.bus;

import java.util.HashMap;
import java.util.Iterator;

import android.content.Context;
import com.ccx.coachsite.http.HttpCallBack;
import com.ccx.coachsite.http.HttpRequest;
import com.ccx.coachsite.http.UrlConfig;
import com.ccx.coachsite.task.TaskBase;
import com.ccx.coachsite.ui.login.LoginHelper;
import com.ccx.coachsite.util.SharedPreferencesUtil;

public class CommonBus extends BaseBus{
	
	private String mUrl;
	protected String mMobile;
	protected String mUserId;
	protected int mUserType;
	private HashMap<String, Object> mMaps;

	public CommonBus(Context context)
	{
		super(context);
		
		mMobile = SharedPreferencesUtil.getValue(context, SharedKey.Shared_Mobile_Key, mMobile);
		mUserId = SharedPreferencesUtil.getValue(context, SharedKey.Shared_UserId_Key, mUserId);
		boolean isExport = SharedPreferencesUtil.getBoolean(mContext, SharedKey.Shared_Is_Expert);
		mUserType = isExport ? 0 : 1;
	}
	
	public void setMaps(HashMap<String, Object> maps)
	{
		this.mMaps = maps;
	}
	
	public void setSendMessageParam(String userId, String recId, String message)
	{
		successInfo = "发送成功";
		errorInfo = "发送失败";
		processInfo = "正在发送...";
		mUrl = UrlConfig.getUrlByKey(UrlConfig.ManageSendMessageUrlKey);
		this.mMaps = new HashMap<String, Object>();
		mMaps.put("message", message);
		
		mUrl += "?senderId="+mUserId + "&receiverId="+recId;
	}
	
	public void setForgetPwdParam(String Mobile, String code)
	{
		successInfo = "您的密码已经通过短信发送到手机";
		errorInfo = "提交失败";
		errorEmptyInfo = "提交失败,号码未注册";
		processInfo = "正在提交...";
		mUrl = UrlConfig.getUrlByKey(UrlConfig.OwerForgetPasswordUrl);
		mUrl += "?mobile="+Mobile+"&code="+code;
	}
	
	public void setSuggestParam(String suggest, String phone)
	{
		successInfo = "提交成功";
		errorInfo = "提交失败";
		processInfo = "正在提交...";
		mUrl = UrlConfig.getUrlByKey(UrlConfig.SuggestUrlKey);
		this.mMaps = new HashMap<String, Object>();
		mMaps.put("content", suggest);
		mMaps.put("contact", phone);
		
		mUrl += "?userId="+mUserId + "&mobile="+mMobile+"&userType="+mUserType;
		mUrl += "&system=android&clientVersion="+LoginHelper.getVersionName(mContext);
	}
	
	public void setInviteParam(String userId, String phones)
	{
		successInfo = "邀请成功";
		errorInfo = "邀请失败";
		processInfo = "正在邀请...";
		mUrl = UrlConfig.getUrlByKey(UrlConfig.InviteUrlKey);
		mUrl += "?userId="+userId;
		this.mMaps = new HashMap<String, Object>();
		//mMaps.put("userId", userId);
		mMaps.put("mobile", phones);
	}
	
	public void setDeleteMessageParam(String messages)
	{
		successInfo = "删除成功";
		errorInfo = "删除失败";
		processInfo = "正在删除消息...";
		mUrl = UrlConfig.getUrlByKey(UrlConfig.OwerDeleteMessageUrlKey);
		mUrl += "?mobile="+mMobile+"&messageId="+messages;
	}
	
	public void setDeleteCollectParam(String id)
	{
		successInfo = "删除成功";
		errorInfo = "删除失败";
		processInfo = "正在删除收藏...";
		mUrl = UrlConfig.getUrlByKey(UrlConfig.OwerDeleteCollectUrlKey);
		mUrl += "?userId="+mUserId+"&id="+id;
	}
	
	public void setScoreParam(int score)
	{
		successInfo = "提交成功";
		errorInfo = "提交失败";
		processInfo = "正在提交...";
		mUrl = UrlConfig.getUrlByKey(UrlConfig.ScoreUrlKey);
		mUrl += "?userId="+mUserId + "&mobile="+mMobile+"&score="+score;
	}
	
	public void setQueryCustomParam(String phone)
	{
		successInfo = "查询成功";
		errorInfo = "查询失败";
		processInfo = "正在查询...";
		mUrl = UrlConfig.getUrlByKey(UrlConfig.ManageQueryContactUrlKey);
		mUrl += "?mobile="+phone;
	}
	
	public void setAddCustomParam(String phone, String userId, int sex, int age, int profession, String hobby)
	{
		successInfo = "添加成功";
		errorInfo = "添加失败";
		processInfo = "正在添加...";
		//mUrl = UrlConfig.getUrlByKey(UrlConfig.ManageAddCustomUrlKey);
		mUrl = UrlConfig.getUrlByKey(UrlConfig.AddCustomExfoUrlKey);
		mUrl += "?mobile="+phone+"&userId="+userId;
		//mUrl += "?phone="+phone+"&sex="+sex+"&age="+age+"&profession="+profession+"&hobby="+hobby;
	}
	
	public void setVipScoreParam(int vipId, int score)
	{
		successInfo = "提交成功";
		errorInfo = "提交失败";
		processInfo = "正在提交...";
		mUrl = UrlConfig.getUrlByKey(UrlConfig.VipScoreUrlKey);
		mUrl += "?userId="+mUserId + "&mobile="+mMobile+"&score="+score+"&expertId="+vipId;
	}
	
	public void doTask() {
		showProgress();
		CommonTask task = new CommonTask(this, mContext, mUrl);
		task.setMaps(this.mMaps);
		task.execute();
	}
	
	public class CommonTask extends TaskBase {

		private String mUrl;
		private HashMap<String, Object> mMaps;
		
		public CommonTask(HttpCallBack<?> iCall, Context context, String url) {
			super(iCall ,context);
			mUrl = url;
		}
		
		public void setMaps(HashMap<String, Object> maps)
		{
			this.mMaps = maps;
		}

		@Override
		protected String genUrl(HttpRequest request)
		{
			if(mMaps != null)
			{
				Iterator<java.util.Map.Entry<String, Object>> ite= mMaps.entrySet().iterator();
		        while(ite.hasNext()){
		        	java.util.Map.Entry<String, Object> entry = (java.util.Map.Entry<String, Object>) ite.next(); 
				    Object key = entry.getKey(); 
				    Object val = entry.getValue(); 
				    request.addParameter(key.toString(), val.toString());
				} 
			}
			return mUrl;
		}
	}
}
